﻿using System;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.SharePoint;
using Microsoft.VisualStudio.SharePoint.Deployment;
using System.IO;
using CodeCounsel.SharePoint.DevTools.Commands;

namespace CodeCounsel.SharePoint.DevTools.DeploymentSteps
{
    [Export(typeof(IDeploymentStep))]
    [DeploymentStep(DeploymentStepIds.UpgradeSolution)]
    public class UpgradeSolution
        : IDeploymentStep
    {
        public void Initialize(IDeploymentStepInfo stepInfo)
        {
            stepInfo.Name = "Upgrade Solution";
            stepInfo.StatusBarMessage = "Upgrading Solution...";
            stepInfo.Description = "Upgrades the solution package.";
        }

        public bool CanExecute(IDeploymentContext context)
        {
            return true;
        }

        public void Execute(IDeploymentContext context)
        {
            context.Project.SharePointConnection.ExecuteCommand<SolutionInfo>(
                CommandIDs.UpgradeSolution,
                new SolutionInfo()
                {
                    IsSandboxedSolution = context.Project.IsSandboxedSolution,
                    LocalPath = context.Project.Package.OutputPath,
                    Name = context.Project.Package.Model.Name + ".wsp"
                });
        }
    }
}
